<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Kotan page model.
 * @author bagnz0r
 * @package Kotan
 */

class Model_Page extends ORM {
	
	/**
	 * Override the default table name for ORM.
	 * 
	 * @var string
	 */
	protected $_table_name = 'pages';
	
	/**
	 * Override validation rules.
	 * 
	 * (non-PHPdoc)
	 * @see modules/orm/classes/kohana/Kohana_ORM::rules()
	 */
	public function rules()
	{
		return array(
			'title' => array(
				array('not_empty'),
				array('min_length', array(':value', 2)),
				array('max_length', array(':value', 160)),
			),
			'content' => array(
				array('not_empty'),
				array('min_length', array(':value', 2)),
				array('max_length', array(':value', 1024 * 16)),
			),
		);
	}
	
	/**
	 * Add new page.
	 * Throws ORM_Validation_Exception.
	 * Param is actually a $_POST array you have to pass.
	 * 
	 * @param array $post
	 */
	public function add_page($post)
	{
		// Set the values
		$this->values(array(
			'title' => Security::xss_clean($post['title']),
			'content' => Security::xss_clean($post['content'], false),
			'user' => Auth::instance()->get_user()->username,
		));
		
		// Save values
		$this->save();
	}
}